package cn.com.thingo.shyintelligent.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.th.opensdk.openapi.RemoterInfo;
import com.th.opensdk.openapi.RemoterKeyInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.thingo.shyintelligent.R;

public class MusicControlAdapter extends RecyclerView.Adapter<MusicControlAdapter.ViewHolder> {
    private List<RemoterInfo> list;
    private Context context;

    public MusicControlAdapter(List<RemoterInfo> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_music_list, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        final RemoterInfo remoterInfo = list.get(position);
        holder.tvName.setText(remoterInfo.getRemoterName());
        final List<RemoterKeyInfo> keys = new ArrayList<>();
        final HashMap<Integer, RemoterKeyInfo> keyList = remoterInfo.getKeyList();
        if (keyList != null && keyList.size() > 0) {
            Set<Integer> integers = keyList.keySet();
            for (Integer next : integers) {
                RemoterKeyInfo remoterKeyInfo = keyList.get(next);
                keys.add(remoterKeyInfo);
            }
        }
//        holder.ivPower.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                int state = remoterInfo.getState();
//                if (state == 0) {
//                    execute(keys, remoterInfo, "开");
//                } else {
//                    execute(keys, remoterInfo, "关");
//                }
//            }
//        });
        holder.ivVoiceUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                execute(keys, remoterInfo, "音量+");
            }
        });
        holder.ivVoiceDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                execute(keys, remoterInfo, "音量-");
            }
        });
        holder.ivPreview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                execute(keys, remoterInfo, "上一曲");
            }
        });
        holder.ivNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                execute(keys, remoterInfo, "下一曲");
            }
        });
        holder.ivPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                execute(keys, remoterInfo, "播放");
            }
        });
        holder.ivPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                execute(keys, remoterInfo, "暂停");
            }
        });
    }

    private void execute(List<RemoterKeyInfo> keys, RemoterInfo remoterInfo, String s) {
        for (int i = 0; i < keys.size(); i++) {
            if (TextUtils.equals(keys.get(i).getKeyName(), s)) {
                remoterInfo.sendRemoterCode(keys.get(i).getKeyId(), RemoterKeyInfo.REMOTER_ACTION_DIRECT, 0);
            }
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_music_name)
        TextView tvName;
//        @BindView(R.id.item_music_power)
//        ImageView ivPower;
        @BindView(R.id.item_music_voice_up)
        ImageView ivVoiceUp;
        @BindView(R.id.item_music_voice_down)
        ImageView ivVoiceDown;
        @BindView(R.id.item_music_preview)
        ImageView ivPreview;
        @BindView(R.id.item_music_next)
        ImageView ivNext;
        @BindView(R.id.item_music_play)
        ImageView ivPlay;
        @BindView(R.id.item_music_pause)
        ImageView ivPause;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
